package org.example.blog_manage.service;


import lombok.extern.slf4j.Slf4j;
import org.example.blog_manage.mapper.IBlogInfoMapper;
import org.example.blog_manage.mapper.IUserInfoMapper;
import org.example.blog_manage.model.BlogInfo;
import org.example.blog_manage.model.UserInfo;
import org.example.blog_manage.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserInfoService {
    @Autowired
    private IUserInfoMapper iUserInfoMapper;

    @Autowired
    private IBlogInfoMapper iBlogInfoMapper;


    public UserInfo selectByUserName(UserInfo userInfo){
        UserInfo userInfo1 = iUserInfoMapper.selectByUserName(userInfo);
        if(userInfo1!=null){
            log.info("返回的结果：userInfo1{}",userInfo1);
            return userInfo1;
        }
        return null;
    }

    public UserInfo selectById(Integer id) {
        if(id==null||id<=0){
            return null;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        return iUserInfoMapper.selectById(userInfo);
    }


    /*
     * 根据博客id查找到博客作者信息
     * */
    public UserInfo selectAuthorByBlogId(Integer blog){
        BlogInfo blogInfo = iBlogInfoMapper.selectById(blog);
        Integer userId = blogInfo.getUserId();
        //根据id（userid）查找作者id
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        return iUserInfoMapper.selectById(userInfo);
    }


    /*
    * 注册
    * */
    public String enroll(UserInfo userInfo) {
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setUserName(userInfo.getUserName());
        String password = SecurityUtil.encrypt(userInfo.getPassword());
        userInfo1.setPassword(password);
        userInfo1.setEmail(userInfo.getEmail());

        Integer n = iUserInfoMapper.insertUserInfo(userInfo1);
        if(n==1){
            return "注册成功";
        }else {
            return "注册失败";
        }

    }
}
